package com.zf.yichat.controller.lottery;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.UserLotteryPrizeDto;
import com.zf.yichat.mapper.LotteryPrizeMapper;
import com.zf.yichat.mapper.UserLotteryPrizeMapper;
import com.zf.yichat.model.LotteryPrize;
import com.zf.yichat.model.UserLotteryPrize;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户抽奖
 */
@Controller
@RequestMapping("lottery")
@Slf4j
public class LotteryController extends BaseController {

    @Resource
    private UserLotteryPrizeMapper userLotteryPrizeMapper;
    @Resource
    private LotteryPrizeMapper lotteryPrizeMapper;
    /**
     * 抽奖列表
     * @return
     */
    @RequestMapping("/page/list")
    public String index() {
        return "lottery/list";
    }

    /**
     * 用户已抽奖列表
     * @return
     */
    @RequestMapping("/page/userLotteryList")
    public String redeemedGoodsList() {
        return "lottery/userLotteryList";
    }

    /**
     * 新增抽奖
     * @return
     */
    @RequestMapping("/page/add")
    public String add() {
        return "lottery/add";
    }

    /**
     * 编辑抽奖
     * @return
     */
    @RequestMapping("/page/edit")
    public String edit(Integer id) {

        LotteryPrize prize = lotteryPrizeMapper.selectByPrimaryKey(id);
        getRequest().setAttribute("prize", prize);
        return "lottery/edit";
    }


    /**
     * 抽奖奖品列表
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public FsResponse goodsList(Integer page, Integer limit, String prizeName, String state) {
        Example example = new Example(LotteryPrize.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(prizeName)) {
            criteria.andLike("prizeName", "%" + prizeName + "%");
        }
        if (StringUtils.isNotBlank(state)) {
            criteria.andEqualTo("state", state);
        }

        PageHelper.startPage(page, limit);
        return DtoChangeUtils.getPageList(lotteryPrizeMapper.selectByExample(example), data -> {
            LotteryPrize goods = new LotteryPrize();
            BeanUtils.copyProperties(data, goods);
            return goods;
        });
    }

    /**
     * 查询已兑换抽奖列表
     * @param page
     * @param limit
     * @param userId
     * @param shipmentTime
     * @param status
     * @return
     */
    @RequestMapping("/queryRedeemedLotteryList")
    @ResponseBody
    public FsResponse queryRedeemedLotteryList(Integer page, Integer limit,
                                             String prizeName,Long userId,String nickName,
                                             String createTime,
                                             String shipmentTime,
                                             String status) {

        PageHelper.startPage(page, limit);
        List<UserLotteryPrizeDto> list = userLotteryPrizeMapper.queryRedeemedGoodsShipmentList(prizeName, userId,nickName, status, createTime, shipmentTime);
        return DtoChangeUtils.getPageList(list, data -> {
            UserLotteryPrizeDto shipmentDto = new UserLotteryPrizeDto();
            BeanUtils.copyProperties(data, shipmentDto);
            return shipmentDto;
        });
    }

    /**
     * 新增抽奖
     * @param prize
     * @return
     */
    @RequestMapping("/addLotteryPrize")
    @ResponseBody
    public FsResponse addGoods(LotteryPrize prize) {

        prize.setCreateTime(new Date());
        lotteryPrizeMapper.insert(prize);
        return FsResponseGen.successMsg("添加成功！");
    }

    /**
     *  编辑抽奖
     * @return
     */
    @RequestMapping("/editLotteryPrize")
    @ResponseBody
    public FsResponse editGoods(LotteryPrize prize) {
        lotteryPrizeMapper.updateByPrimaryKeySelective(prize);
        return FsResponseGen.successMsg("编辑成功！");
    }

    /**
     *  删除抽奖
     * @param prizeId
     * @return
     */
    @RequestMapping("/delLotteryPrize")
    @ResponseBody
    public FsResponse delGoods(Integer prizeId) {
        lotteryPrizeMapper.deleteByPrimaryKey(prizeId);
        return FsResponseGen.successMsg("删除成功！");
    }


    /**
     *  已兑换抽奖发货功能
     * @param id
     * @return
     */
    @RequestMapping("/redeemedLotteryShipment")
    @ResponseBody
    public FsResponse redeemedLotteryShipment(Long id, String logistics) {

        UserLotteryPrize prize = new UserLotteryPrize();
        prize.setId(id);
        prize.setStatus(UserLotteryPrize.STATE_1);
        prize.setShipmentTime(new Date());
        prize.setLogistics(logistics);
        return FsResponseGen.successData(userLotteryPrizeMapper.updateByPrimaryKeySelective(prize));
    }
}
